// pages/components/judge/judge.js
let app = getApp();
import http from '../../../services/http/index';
import Toast from "../toast/toast.js";

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 组件隐藏属性
    'hide': {
      'type': Boolean,
      'value': true,
      observer: '_initStar'
    },
    'succCal':{
      'type':null
    },
    'ordersId':{
      'type':null,
      'value':''
    },
    'ordersType':{
      'type':null,
      'value':'0'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    stars: [false, false, false, false, false],
    imgs: [],
    score:0,
    remark:''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    _initStar() {
      console.log(this.data.ordersId);
      var imgs = [], stars = this.data.stars,score = 0;
      for (var i = 0; i < 5; i++) {
        if (stars[i]) {
          score++
          imgs.push('../../../images/icon_home_star_light@2x.png')
        } else {
          imgs.push('../../../images/icon_home_star_grey.png')
        }
      };
      this.setData({
        'imgs': imgs,
        'score':score
      })
    },
    _bindTextAreaBlur(e){
      var val = e.detail.value;
      this.setData({
        'remark':val
      });
    },
    _tapStar(e) {
      let index = e.currentTarget.dataset.index;
      var newS = []
      for (var i = 0; i < 5; i++) {
        if (i <= index) {
          newS.push(true)
        } else {
          newS.push(false)
        }
      }
      this.setData({
        'stars': newS
      });
      this._initStar();
    },
    // 隐藏组件
    maskToggle() {
      this.setData({
        'stars': [false, false, false, false, false],
        'imgs': [],
        'score':'',
        'remark': ''
      });
      this._initStar();
      app.maskToggle(this);
    },
    _submit(){
      if(this.data.score == 0)return;
      var _this = this;
      let param = { id: this.data.ordersId, star: this.data.score, comment:this.data.remark };
      wx.showToast({
        title: '请求中',
        icon: 'loading',
        duration: 10000
      });

      var succCal = function(data){
        wx.hideToast();
        _this.triggerEvent('succCal');
        _this.maskToggle();
        wx.showToast({
          title: '评价成功',
          duration: 2000
        })
      };

      var errCal = function(error){
        wx.hideToast();
        //wx.showToast({
        //  icon:'none',
        //  title: error.message || '评价失败',
        //  duration: 2000
        //});
        Toast.show(_this,'info',error.message || '评价失败')
      }

      if(this.data.ordersType == 0){
        //接机单
        http.comment(param)
            .then(data => {
              succCal(data);
            }).catch(error => {
          errCal(error)
        })
      }

      if(this.data.ordersType == 3){
        //预约用车
        http.appointComment(param)
            .then(data => {
              succCal(data);
            }).catch(error => {
          errCal(error)
        })
      }

      // if(this.data.ordersType == 3){
      //   //包车
      //   http.charterComment(param)
      //       .then(data => {
      //         succCal(data);
      //       }).catch(error => {
      //     errCal(error)
      //   })
      // }


    }
  },
  ready() {
   
  }
})
